/*
 *  $Id: Marauder.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Darkness.Creatures {
    public class Marauder: CreatureCard {
        public Marauder(): base("001_d_c_marauder") {
            Level = CompleteLevel.ReadyForTest;
            Version = 1;
            Element = ElementType.Darkness;
            Damage = 3;
            Health = 3;
            AddManaCost(ElementType.Darkness, 2);
        }

        public override void SetNaturalAffects(ICollector<Affect> affects) {
            affects.Add<MarauderAffect>();
        }

        private const int MarauderCost = 1;
        private const int MarauderRefund = 1;

        private class MarauderAffect: Affect {
            private static readonly Languages.CardPhrase<Marauder> Pay = new Languages.CardPhrase<Marauder>("pay");

            private bool _payed = false;

            public override string Id {
                get { return AffectManager.GetCardId<Marauder>(null); }
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnStartOffencePhase += DoPhase;
                LocalSummary.OnStartDefencePhase += DoPhase;
                LocalSummary.OnAttackTry += DoAttack;
                LocalSummary.OnDefenceTry += DoDefence;
                LocalSummary.OnHit += DoHit;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnStartOffencePhase -= DoPhase;
                LocalSummary.OnStartDefencePhase -= DoPhase;
                LocalSummary.OnAttackTry -= DoAttack;
                LocalSummary.OnDefenceTry -= DoDefence;
                LocalSummary.OnHit -= DoHit;
            }

            private void DoPhase() {
                _payed = false;
            }

            private void DoAttack(Creature attacker, ref bool canAttack) {
                PayCost(attacker);
                canAttack = canAttack && _payed;
            }
            private void DoDefence(Creature defender, Creature attacker, ref bool canDefence) {
                PayCost(defender);
                canDefence = canDefence && _payed;
            }
            private void PayCost(Creature marauder) {
                if (_payed) return;
                _payed = marauder.Owner.GetMana(ElementType.Darkness) >= MarauderCost;
                if (_payed) {
                    Game.Current.Log.Add(Pay.Phrase, marauder.Owner, marauder, MarauderCost);
                    marauder.Owner.DecrementMana(ElementType.Darkness, MarauderCost);
                } else
                    Game.Current.ErrorNotice(ManaCondition.NotEnough.GetText(ElementType.Darkness));
            }

            private void DoHit(Creature hitter, AbstractObject target, int damage) {
                if (target is Creature && target.IsDeath) {
                    ElementType element = ((Creature) target).Card.Element;
                    if (element == ElementType.Neutral)
                        hitter.Owner.CardToHands(false, true);
                    else
                        hitter.Owner.IncrementMana(element, MarauderRefund);
                }
            }
        }
    }
}
